<?php
	$paging = new pages($host,$user,$pass,$db_name);
	//Get list services
	$paging->select("id,name");
	$paging->order("name");
	$list_questions = $paging->get_data("question");
	$paging->reset_data();
	
	$uri = parse_url($_SERVER['REQUEST_URI']);
	
	if(preg_match("/page-/",$action)){
		$current_page = (int)substr($action,5);
		parse_str($uri['query'],$q);
		$keyword = $q['q'];
	}
	if($current_page < 1) $current_page = 1;
	if(isset($_POST['request_search'])){
		$keyword = $_POST['keyword'];
		$current_page = 1;
	}
	$keyword = preg_replace("/[\/\'\"]/","",$keyword);
	$paging->setKeyword($keyword);
	
	$paging->setCurrentPage($current_page);
	$view_per_page	= $paging->getViewPerPage() + $paging->getSegment();
	$position_start = ($current_page-1)*$view_per_page;
	
	$paging->select("question.name as qname,question.level,answers.id as aid,answers.qid,answers.title,answers.answer,answers.image");
	$paging->join("join question on question.id = answers.qid");
	
	if($keyword)
		$paging->where("answers.title like '%$keyword%'");
	
	$num_rows = $paging->count_rows("answers");
	if($num_rows)
		$total_record = $num_rows['count'];
		
	$paging->order("qname,answers.answer");
	$paging->limit("$position_start,$view_per_page");
	$list_answers = $paging->get_data("answers");
	
	$paging->setTotalRecords($total_record);
	$total_page		= $paging->getTotalPage();
	$paging->setLink("$home_url/admin/answers");
	
	$paging->setListPage($router,position_start);
	$first_page		= $paging->getFirstPage();
	$last_page		= $paging->getLastPage();
	$prev_page		= $paging->getPreviousPage();
	$next_page		= $paging->getNextPage();
	$list_page		= $paging->getListPage();
	
?>

<div class="container" id="page-answer">
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12 text-center">
			<form role="form" class="form-horizontal" method="post" action="<?php echo $home_url; ?>/admin/answers/new">
				<fieldset class="margin20">
					<legend class="text-center padding10-bottom">NHẬP CÂU TRẢ LỜI MỚI</legend>
					<?php
						$notice = parse_url($_SERVER['REQUEST_URI']);
						parse_str($notice['query'],$alert);
						
						if($alert['notice'] == 'success'){
							showAlert("alert-success","Thêm câu trả lời thành công.");
						}elseif($alert['notice'] == 'error'){
							showAlert("alert-danger alert-error","Thêm câu trả lời thất bại.");
						}elseif($alert['notice'] == 'duplicate'){
							showAlert("alert-danger alert-error","Câu hỏi này đã có.");
						}elseif($alert['notice'] == 'delete_success'){
							showAlert("alert-success","Xóa câu trả lời thành công.");
						}elseif($alert['notice'] == 'delete_error'){
							showAlert("alert-danger alert-error","Xóa câu trả lời thất bại.");
						}elseif($alert['notice'] == 'update_success'){
							showAlert("alert-success","Cập nhật câu trả lời thành công.");
						}elseif($alert['notice'] == 'update_error'){
							showAlert("alert-danger alert-error","Xóa câu trả lời thất bại.");
						}
    
					?>
					<!--
					<div class="form-group">
						<label for="answer_type" class="control-label col-xs-3">Loại câu trả lời</label>
						<div class="col-xs-6">
							<select name="answer_type" id="answer_type" class="form-control">
								<option value="1">Text</option>
								<option value="2">Hình</option>
							</select>
						</div>
					</div> -->
					<div class="form-group">
						<label for="answer_name" class="control-label col-xs-3">Câu trả lời</label>
						<div class="col-xs-6">
							<input type="text" name="answer_name" id="answer_name" class="form-control" size="40" placeholder="Nhập câu trả lời..." />
						</div>
					</div>
					<div class="form-group">
						<label for="answer_answer" class="control-label col-xs-3">Đáp án</label>
						<div class="col-xs-6">
							<select name="answer_answer" id="answer_answer" class="form-control">
								<option value="0">Sai</option>
								<option value="1">Đúng</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="answer_qid" class="control-label col-xs-3">Thuộc câu hỏi</label>
						<div class="col-xs-6">
							<select name="answer_qid" id="answer_qid" class="form-control">
							<?php
								if($list_questions != null){
									foreach($list_questions as $question){
										echo "<option value='".$question['id']."'>".$question['name']."</option>";
									}
								}else{
									echo "<option value=-1>Chưa có câu hỏi nào</option>";
								}
							?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-xs-offset-3 col-xs-6">
							<button type="submit" name="request_add" id="request_add" class="btn btn-primary">Thêm câu trả lời</button>
						</div>
					</div>
				</fieldset>
			</form>
		</div>
	</div>
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12">
			<div class="center-block">
				<form action="<?php echo $home_url;?>/admin/answers/" method="POST" role="form" class="form-inline text-center">
					<div class="form-group">
						<label for="keyword">Tìm câu trả lời:</label>
						<input type="text" name="keyword" id="keyword" class="form-control" size="60" value="<?php echo $keyword;?>" placeholder="Nhập tên câu trả lời cần tìm..." />
						<button type="submit" name="request_search" id="request_search" class="btn btn-primary">
							<i class="fa fa-search fa-lg"></i>
						</button>
					</div>
				</form>
			</div>
			<div class="center-block table-list">
				<div class="title text-center text-primary">
					<h2>DANH SÁCH CÂU TRẢ LỜI</h2>
				</div>
				<table role="table" class="table table-bordered table-list">
					<thead class="bg-primary">
						<th width="40" class="text-center">STT</th>
						<th class="text-center">Câu hỏi</th>
						<th class="text-center">Câu trả lời</th>
						<th width="200" class="text-center">Đáp án</th>
						<th class="text-center">Loại câu trả lời</th>
						<th width="100" class="text-center">Thao tác</th>
					</thead>
					<tbody>
					<?php
						if($list_answers != null){
							$stt = $position_start + 1;
							$same_question = true;
							$qid = $answer[0]['qid'];
							foreach($list_answers as $answer){
								
								if($qid != $answer['qid']){
									$qid = $answer['qid'];
									$same_question = !$same_question;
								}
								if($same_question) $class_group = "answer_even";
								else $class_group = "";
								
								echo "<tr class='$class_group'>";
									echo "<td class='text-center'>$stt</td>";
									echo "<td>".$answer['qname']."</td>";
									echo "<td class='text-center'>".$answer['title']."</td>";
									echo "<td class='text-center'>".(($answer['answer'] == 1)? 'Đúng':'')."</td>";
									if($answer['level'] == 1) $atype = "Dễ";
									elseif($answer['level'] == 2) $atype = "Trung bình";
									else $atype = "Khó";
									echo "<td class='text-center'>$atype</td>";
									echo "<td class='text-center'>";
										echo "<a style='position:relative;top:2px' class='padding10-right' href='".$home_url."/admin/answers/edit/".$answer['aid']."' title='Sửa câu trả lời'>";
											echo "<span class='fa fa-edit fa-2x text-primary'></span>";
										echo "</a>";
										echo "<a class='show_confirm' href='".$home_url."/admin/answers/delete/".$answer['aid']."' title='Xóa câu trả lời'>";
											echo "<span class='fa fa-remove fa-2x text-danger'></span>";
										echo "</a>";
									echo "</td>";
								echo "</tr>";
								$stt++;
							}
						}else{
							echo "<tr><td class='text-center text-danger' colspan='6'>Chưa có dữ liệu</td></tr>";
						}
					?>
					</tbody>
				</table>
				<?php
					if($total_page > 1){
						echo "<div class='text-center'>";
							echo "<ul class='pagination pagination-lg'>";
								echo $first_page;
								echo $prev_page;
								foreach($list_page as $l){
									echo $l;
								}
								echo $next_page;
								echo $last_page;
							echo "</ul>";
						echo "</div>";
					}
				?>
			</div>
		</div>
	</div>
</div>
<div id="box-confirm" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">Xác nhận xóa</h4>
			</div>
			<div class="modal-body">
				<p>Bạn có chắc là muốn xóa không?</p>
				<!--<p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>-->
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Không</button>
				<button type="button" class="btn btn-primary confirm">Có</button>
				<input type="hidden" id="confirm_link" value="" />
			</div>
		</div>
	</div>
</div>